import React from 'react';
import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { AreaNumManageAPI } from '../common/type';

/**
 * 行政区域管理业务逻辑
 * @param permissions
 */
const AreaNumManageService = (permissions: Record<string, any>) => {
  /** 表格数据请求处理业务 */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (requestParams: AreaNumManageAPI.ITableRequestType) => {
      const dataGridURL = `${getBaseUrl()}/areaNumController/dataGrid`;
      let postParams = {
        current: 1,
        pageSize: 10,
        sort: 'provinceCode',
        ...requestParams,
      };
      return requestFunComponFiex('POST', dataGridURL, postParams);
    },
    {
      manual: false,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /** 重新请求数据 */
  const refreshRequest = React.useCallback(
    (requestParams: AreaNumManageAPI.ITableRequestType | any) => {
      run({
        ...requestParams,
      });
    },
    [],
  );

  /** 页面数据及分页信息 */
  const areaNumDataInfo = React.useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    refreshRequest,
    areaNumrefresh: refresh,
    ...areaNumDataInfo,
  };
};

/** 管理页面上下文信息 */
const useContextInfo: AreaNumManageAPI.IManageContextType = {
  dataSource: [],
  loading: false,
  refreshRequest: ({ }) => { },
  dicMapList: [],
  pagination: {},
  requestParams: {},
};

const AreaNumManageContext = React.createContext(useContextInfo);

export { AreaNumManageService, AreaNumManageContext };
